import environment from "/environment";
import axios from "axios";

const request = axios.create({
    baseURL: environment,
    timeout: 10000,
    headers: { "X-Custom-Header": "foobar" },
  
    withCredentials: true, // 允许携带cookie
  });
  
  // request 拦截器
  // 可以自请求发送前对请求做一些处理
  // 比如统一加token，对请求参数统一加密
  request.interceptors.request.use(
    (config) => {
      config.headers["Content-Type"] = "application/json;charset=utf-8";
      // debugger
      let token = sessionStorage.getItem("token");
      let userId = sessionStorage.getItem("userId");
      if (token != null || userId != null) {
        config.headers["token"] = token; // 设置请求头
        config.headers["userId"] = userId;
      }
  
      return config;
    },
    (error) => {
      return Promise.reject(error);
    }
  );
  export default request;
